package com.dream.common.service.base;




import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dream.common.dao.base.CommonDao;

@Transactional ( readOnly = true )
public abstract class AbstractService < T , PK extends Serializable >
{
	protected Log				logger	= LogFactory.getLog ( getClass ( ) );
	private CommonDao< T , PK >	commonDao;
	
	protected CommonDao< T , PK > getCommonDao ( )
	{
		return commonDao;
	}
	
	protected void setCommonDao ( CommonDao< T , PK > commonDao )
	{
		this.commonDao = commonDao;
	}
	
	protected abstract Class< T > getClassType ( );
	
	@Transactional ( readOnly = false )
	public T save ( T transientInstance )
	{
		return getCommonDao ( ).save ( transientInstance );
	}
	
	@Transactional ( readOnly = false )
	public T saveHasReturn ( T transientInstance )
	{
		return getCommonDao ( ).saveHasReturn ( transientInstance );
	}
	
	@Transactional ( readOnly = false )
	public T merge ( T transientInstance )
	{
		return getCommonDao ( ).merge ( transientInstance );
	}
	
	@Transactional ( readOnly = false )
	public void delete ( T persistentInstance )
	{
		getCommonDao ( ).delete ( persistentInstance );
	}
	
	@Transactional ( readOnly = false )
	public void deleteById ( PK id )
	{
		getCommonDao ( ).delById ( id );
	}
	
	@Transactional ( readOnly = false , propagation = Propagation.REQUIRED )
	public void delById ( PK id )
	{
		getCommonDao ( ).delById ( id );
	}
	
	@Transactional ( readOnly = false )
	public void delByIds ( PK [ ] ids )
	{
		getCommonDao ( ).delByIds ( ids );
	}
	
	public T findById ( PK id )
	{
		return getCommonDao ( ).findById ( id );
	}
	
	public List< T > findAll ( )
	{
		return getCommonDao ( ).findAll ( );
	}
	
	public List< T > findAll ( int maxResults )
	{
		return getCommonDao ( ).findAll ( maxResults );
	}
}
